package com.yyw.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.Service.AccusationService;
import com.yyw.bean.Accusation;
import com.yyw.bean.User;
import com.yyw.dao.AccusationDao;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "举报API")
@RestController
@RequestMapping("/user")
@Slf4j
public class AccsuationController {

    @Value("${my.session.user}")
    private String userAttr;

    @Autowired
    private AccusationService accusationService;

    @Autowired
    private AccusationDao accusationDao;

    // 获取举报信息以及被举报次数，分页
    @CrossOrigin
    @ApiOperation(value = "获取举报信息以及被举报次数，分页", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getAccusationGroup")
    public Result getAccusationGroup(
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageNum", required = false, defaultValue = "10") Integer pageNum) {
        PageHelper.startPage(pageNo, pageNum);
        List<Accusation> list = accusationDao.getAccusationsGroup();
        PageInfo<Accusation> pageInfo = new PageInfo<>(list);
        return ResultFactory.success(pageInfo);
    }

    // 处理举报信息，根据aid删除
    @CrossOrigin
    @ApiOperation(value = "处理举报信息，根据aid删除", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "aid", value = "举报物id", required = true) })
    @PostMapping("/dealAccusation")
    @Transactional
    public Result dealAccusation(@RequestParam("aid") Long aid) {
        if (aid == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = accusationDao.deleteAccusation(aid);
        return ResultFactory.success("删除成功");
    }

    @CrossOrigin
    @ApiOperation(value = "举报短文|分享文|评论", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "type", value = "举报物类型", required = true),
            @ApiImplicitParam(name = "aid", value = "举报物id", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/saveAccsuation")
    public Result saveAccsuation(@RequestParam("type") Integer type, @RequestParam("id") Long aid,
            @RequestParam("content") String content, @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute(userAttr);
        Accusation accusation = new Accusation(null, user.getId(), type, aid, content, null, null, null);
        return accusationService.saveAccusation(accusation);
    }
}
